// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameData from "./GameData";
import GameScene from "./GameScene";
import { PoolManager } from "./PoolManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Guest extends cc.Component {

    @property(cc.Sprite)
    patient: cc.Sprite = null;

    @property(cc.Sprite)
    head: cc.Sprite = null;

    @property(cc.Sprite)
    body: cc.Sprite = null;

    @property(cc.Animation)
    ani: cc.Animation = null;

    @property(cc.Node)
    ask: cc.Node = null;

    @property([cc.SpriteFrame])
    headSf: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    bodySf: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    angrySf: cc.SpriteFrame[] = [];

    characterNum = 0;
    num = -1;
    AskNum = 0;
    GetNum = 0;
    patientPart = 0;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {

    }

    protected onEnable(): void {
        this.characterNum = Math.floor(GameData.randomRange(0, 12));
        // this.characterNum = 2;
        this.head.spriteFrame = this.headSf[this.characterNum];
        this.body.spriteFrame = this.bodySf[this.characterNum];
    }

    // update (dt) {}

    Walk(pos: cc.Vec3) {
        this.ani.play("Walk");
        cc.tween(this.node)
            .to(3, { position: pos })
            .call(() => {
                this.node.setParent(GameScene.Instance.guestWait);
            })
            .to(2, { scale: 0.8 })
            .call(() => {
                this.Ask();
            })
            .start();
    }

    Ask() {
        // this.AskNum = Math.floor(GameData.randomRange(0, GameData.clamp(3, 1, GameData.Instance.Datas[1] + 1)));
        this.AskNum = 0;
        this.GetNum = 0;
        cc.tween(this.ask)
            .to(0.25, { scale: 0.7 })
            .call(() => {
                for (let i = 0; i < this.ask.children[0].children.length; i++) {
                    const element = this.ask.children[0].children[i];
                    if (i <= this.AskNum) element.active = true;
                }
            })
            .start();
        this.Wait();
    }

    Wait() {
        this.ani.play("Wait");
        this.patientPart = 0;
        this.patient.fillRange = 0;
        cc.tween(this.patient.node.parent)
            .to(0.25, { opacity: 255 })
            .start();
        this.schedule(() => {
            this.patientPart += 0.1;
            this.patient.fillRange = this.patientPart;
            console.log(this.patientPart);
            if (this.patientPart == 0.5) this.head.spriteFrame = this.angrySf[this.characterNum];
            if (this.patientPart > 0.95) this.Leave();
        }, 3, 9);
    }

    GetSandwich() {
        this.GetNum++;
        if (this.GetNum > this.AskNum) this.GetLeave();
    }

    GetLeave() {
        this.unscheduleAllCallbacks();
        this.head.spriteFrame = this.headSf[this.characterNum];
        this.Leave(true);
    }

    Leave(isFinish = false) {
        if (isFinish) GameScene.Instance.GuestCoin(this.num);
        GameScene.Instance.guestDatas[this.num] = null;
        cc.tween(this.patient.node.parent)
            .to(0.25, { opacity: 0 })
            .start();
        cc.tween(this.ask)
            .to(0.25, { scale: 0 })
            .start();
        var num = Math.floor(GameData.randomRange(0, GameScene.Instance.guestSummons.length));
        var pos = GameScene.Instance.guestSummons[num].position;
        this.ani.play("Walk");
        cc.tween(this.node)
            .to(2, { scale: 0.6 })
            .to(3, { position: pos })
            .call(() => {
                this.scheduleOnce(() => {
                    PoolManager.putNode(this.node);
                });
            })
            .start();
    }
}
